package com.github.slprj.slake.pb;

import java.nio.ByteBuffer;

/**
 * @author YangXiaoLong
 * @date 2022/9/9 11:45
 */
public class Frame {
    public static final int IDX_TYPE = 1;
    public static final int IDX_COMMAND = 2;
    public static final int IDX_SEQ = 3;
    public static final int IDX_RESP_CODE = 4;
    public static final int IDX_MSG = 5;
    public static final int IDX_BODY = 15;

    private int frameType;
    private int command;
    private int seq;
    private int respCode;
    private String msg;
    private ByteBuffer body;
    /**
     * 数据的大小
     */
    private int pbSize;

    public int pbSize() {
        if (pbSize == 0) {
            //msgBytes 可以为汉字, 汉字utf-8 占3字节
            int msgBytes = msg == null ? 0 : msg.length() * 3 + (1 + 5);
            int bodyBytes = body == null ? 0 : body.remaining() + (1 + 5);
            //(1 + 5) * 4 固定字段的长度
            pbSize = (1 + 5) * 4 + msgBytes + bodyBytes;
        }
        return pbSize;
    }

    public ByteBuffer toByteBuffer() {
        int s = pbSize();
        ByteBuffer buf = ByteBuffer.allocateDirect(s + 4);
        buf.position(4);
        if (frameType != 0) {

        }
        if (command != 0) {

        }
        if (seq != 0) {

        }
        if (respCode != 0) {

        }
        if (msg != null && msg.length() > 0) {

        }
        if (body != null && body.remaining() > 0) {

        }


        return buf;
    }

}
